//
//  TYPZJXViewController.m
//  Estate
//
//  Created by heyong on 16/5/27.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "TYPZJXViewController.h"
#import "B_JixieCell.h"
#import "B_SwitchCell.h"

@interface TYPZJXViewController ()
@end

@implementation TYPZJXViewController
-(NSMutableArray *)selectedAssetURLs{
    if(!_selectedAssetURLs){
        _selectedAssetURLs=[NSMutableArray array];
    }
    return _selectedAssetURLs;
}
-(Site_01 *)site{
    if(_site==nil){
        _site=[[Site_01 alloc]init];
    }
    return _site;
}

- (NSMutableDictionary *)dataDic{
    if(_dataDic==nil){
        _dataDic=[NSMutableDictionary dictionary];
    }
    return _dataDic;
}

-(TPKeyboardAvoidingTableView *)tableView{
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRECTS(0, 0, 375, 667)];
        _tableView.bounces=NO;
        _tableView.delegate=self;
        _tableView.dataSource=self;
        
        //注册表格
        [LSUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
        [_tableView registerNib:[UINib nibWithNibName:@"B_JixieCell" bundle:nil] forCellReuseIdentifier:@"jixie"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_SwitchCell" bundle:nil] forCellReuseIdentifier:@"switch"];
        [_tableView registerClass:[H1_ImagesCell class] forCellReuseIdentifier:kCellIdentifier_H1_ImagesCell];
    }
    return _tableView;
}
- (UIImagePickerController *)picker{
    if(!_picker){
        _picker = [[UIImagePickerController alloc] init];
        _picker.delegate = self;
       _picker.allowsEditing = NO;//设置可编辑
       _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    return _picker;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.00];
    [self.view addSubview:self.tableView];
    _curTweet = [[Tweet alloc] init];
    [self setData];
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(refresh:) name:@"RefreshImage_1" object:nil];
}

- (void)refresh:(id)sender{
    if(self.selectedAssetURLs!=nil){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            self.curTweet.selectedAssetURLs =self.selectedAssetURLs;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationFade];
            });
        });
        
    }
}

-(void) setData{
    WEAKSELF
    //查看页
    if(_site!=nil){
        [weakSelf.dataDic setObject:_site.jixie_1 forKey:@"设备1名称"];
        [weakSelf.dataDic setObject:_site.jixie_2 forKey:@"设备2名称"];
        [weakSelf.dataDic setObject:_site.jixie_3 forKey:@"设备3名称"];
        [weakSelf.dataDic setObject:_site.jixie1_num forKey:@"设备1台数"];
        [weakSelf.dataDic setObject:_site.jixie2_num forKey:@"设备2台数"];
        [weakSelf.dataDic setObject:_site.jixie3_num forKey:@"设备3台数"];
        [weakSelf.dataDic setObject:_site.isJ forKey:@"是否满足施工组织需要"];
        [weakSelf.dataDic setObject:_site.isJ_1 forKey:@"上述各项机械设备运转是否正常"];
        self.selectedAssetURLs= [LSUtility quertFileImageType:@"RECORD_02_SIDE_02" andId:_site.recordId andFilePathArr:_site.fileArray];
        [self refresh:nil];

        
    }else{
        //设置默认值
        [weakSelf.dataDic setObject:@"" forKey:@"设备1名称"];
        [weakSelf.dataDic setObject:@"" forKey:@"设备2名称"];
        [weakSelf.dataDic setObject:@"" forKey:@"设备3名称"];
        [weakSelf.dataDic setObject:@"" forKey:@"设备1台数"];
        [weakSelf.dataDic setObject:@"" forKey:@"设备2台数"];
        [weakSelf.dataDic setObject:@"" forKey:@"设备3台数"];
        [weakSelf.dataDic setObject:@"是" forKey:@"是否满足施工组织需要"];
        [weakSelf.dataDic setObject:@"是" forKey:@"上述各项机械设备运转是否正常"];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0;
    }else{
        return 10;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 4;
    }else{
        return 1;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==2){
         return  [H1_ImagesCell cellHeightWithObj:_curTweet title:@"附件"];
    }else{
        return 44;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   WEAKSELF
    if(indexPath.section==0){
        if(indexPath.row==0){
            B_JixieCell *cell=[tableView dequeueReusableCellWithIdentifier:@"jixie" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            
            //设置设备名
            cell.titleValueChangedBlock=^(NSString *valuerStr){
                weakSelf.site.jixie_1=valuerStr;
                [self.dataDic setObject:valuerStr forKey:@"设备1名称"];
            };
            //设置数量
            cell.numberValueChangedBlock=^(NSString *valuerStr){
                weakSelf.site.jixie1_num=valuerStr;
                [self.dataDic setObject:valuerStr forKey:@"设备1台数"];
            };
            cell.isEdit=weakSelf.isEdit;
            if(_site!=nil){
                [cell configWithPlaceholder:@"机械名称" andValue:_site.jixie_1 andNumPh:_site.jixie1_num andNumValue:@"" andLabel:@"台" andKeyType:UIKeyboardTypeNumberPad];
            }else{
                [cell configWithPlaceholder:@"机械名称" andValue:@"" andNumPh:@"请输入数量" andNumValue:@"" andLabel:@"台" andKeyType:UIKeyboardTypeNumberPad];
            }

            
            return cell;
        }else if(indexPath.row==1) {
            B_JixieCell *cell=[tableView dequeueReusableCellWithIdentifier:@"jixie" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            //设置设备名
            cell.titleValueChangedBlock=^(NSString *valuerStr){
                weakSelf.site.jixie_2=valuerStr;
                [self.dataDic setObject:valuerStr forKey:@"设备2名称"];

            };
            //设置数量
            cell.numberValueChangedBlock=^(NSString *valuerStr){
                weakSelf.site.jixie2_num=valuerStr;
                 [self.dataDic setObject:valuerStr forKey:@"设备2台数"];
            };
            cell.isEdit=weakSelf.isEdit;
            if(_site!=nil){
                [cell configWithPlaceholder:@"机械名称" andValue:_site.jixie_2 andNumPh:_site.jixie2_num andNumValue:@"" andLabel:@"台" andKeyType:UIKeyboardTypeNumberPad];
            }else{
                [cell configWithPlaceholder:@"机械名称" andValue:@"" andNumPh:@"请输入数量" andNumValue:@"" andLabel:@"台" andKeyType:UIKeyboardTypeNumberPad];
            }
            return cell;
            
        }else if(indexPath.row==2){
            B_JixieCell *cell=[tableView dequeueReusableCellWithIdentifier:@"jixie" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            //设置设备名
            cell.titleValueChangedBlock=^(NSString *valuerStr){
                 weakSelf.site.jixie_3=valuerStr;
                 [self.dataDic setObject:valuerStr forKey:@"设备3名称"];
            };
            //设置数量
            cell.numberValueChangedBlock=^(NSString *valuerStr){
                 weakSelf.site.jixie3_num=valuerStr;
                [self.dataDic setObject:valuerStr forKey:@"设备3台数"];
            };
            cell.isEdit=self.isEdit;
            if(_site!=nil){
                [cell configWithPlaceholder:@"机械名称" andValue:_site.jixie_3 andNumPh:_site.jixie3_num andNumValue:@"" andLabel:@"台" andKeyType:UIKeyboardTypeNumberPad];
            }else{
                [cell configWithPlaceholder:@"机械名称" andValue:@"" andNumPh:@"请输入数量" andNumValue:@"" andLabel:@"台" andKeyType:UIKeyboardTypeNumberPad];
            }
         
            return cell;
        }else{
            B_SwitchCell *cell=[tableView dequeueReusableCellWithIdentifier:@"switch" forIndexPath:indexPath ];
            cell.title.text=@"是否满足施工组织需要";
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.isJ=sender.on==0?@"否":@"是";
                if(sender.on){
                    [self.dataDic setObject:@"是" forKey:@"是否满足施工组织需要"];
                }else{
                    [self.dataDic setObject:@"否" forKey:@"是否满足施工组织需要"];
                }
            };
            cell.isEdit=self.isEdit;
            if(_site!=nil){
                if([_site.isJ isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;
        }
    }else if(indexPath.section==1){
        B_SwitchCell *cell=[tableView dequeueReusableCellWithIdentifier:@"switch" forIndexPath:indexPath ];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.title.text=@"上述各项机械设备运转是否正常";
        
        cell.sValueChangeBlock=^(UISwitch *sender){
            weakSelf.site.isJ_1=sender.on==0?@"否":@"是";
            if(sender.on){
                [self.dataDic setObject:@"是" forKey:@"上述各项机械设备运转是否正常"];
            }else{
                [self.dataDic setObject:@"否" forKey:@"上述各项机械设备运转是否正常"];
            }
        };
        
        cell.isEdit=self.isEdit;
        
        if(_site!=nil){
            if([_site.isJ_1 isEqualToString:@"是"]){
                //设置开关
                [cell.kaiguan setOn:YES];
            }else{
                [cell.kaiguan setOn:NO];
            }
        }
        return cell;
    }else{
        WEAKSELF
        H1_ImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_H1_ImagesCell forIndexPath:indexPath];
        cell.isEdit=self.isEdit;
        [cell setTitle:@"附件"];
        cell.curTweet = _curTweet;
        cell.addPicturesBlock = ^(){
            [weakSelf showActionForPhoto];
        };
        __block H1_ImagesCell *cellblock = cell;
        cell.deleteTweetImageBlockUrl = ^(TweetImage *toDelete,NSString *deurl){
            [cellblock.curTweet.tweetImages removeObject:toDelete];
            [weakSelf.selectedAssetURLs removeObject:deurl];
            [weakSelf refresh:nil];
        };
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        return cell;
    }
    
}

#pragma mark UIActionSheet M

- (void)showActionForPhoto{
    //    @weakify(self);
    [[UIActionSheet bk_actionSheetCustomWithTitle:nil buttonTitles:@[@"拍照", @"从相册选择"] destructiveTitle:nil cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
        
        [self photoActionSheet:sheet DismissWithButtonIndex:index];
        
    }] showInView:self.view];
}

- (void)photoActionSheet:(UIActionSheet *)sheet DismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        //拍照
        if (_curTweet.tweetImages.count >= 9) {//照片最多这么多
            kTipAlert(@"最多只可选择9张照片，已经选满了。先去掉一张照片再拍照呗～");
            return;
        }
      
        [self presentViewController:self.picker animated:YES completion:nil];//进入照相界面
    }else if (buttonIndex == 1){
        PictureViewController *picCtr=[[PictureViewController alloc]init];
        picCtr.flag=@"file";
        NSMutableArray *array = [self.selectedAssetURLs valueForKeyPath:@"@distinctUnionOfObjects.self"];
        picCtr.selectedImages=[[NSMutableArray alloc] initWithArray:array];
        picCtr.selectedImageBlock=^(NSMutableArray *array){
            for (NSString *path in array) {
                //添加图片路径到已选数组
                [self.selectedAssetURLs addObject:path];
            }            
        };
        [self.navigationController pushViewController:picCtr animated:YES];
        
    }
}

#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    self.curTweet.editImageView = [[EditImageView alloc]initWithImage:image];
    WEAKSELF
    self.curTweet.editImageView.completeBlock = ^(NSString *path) {
        //添加选择附件
        [weakSelf.curTweet addASelectedAssetURL:path];
        [weakSelf.selectedAssetURLs addObject:path];
        //刷新指定行
        [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationFade];
    };
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark scrollview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        [self.view endEditing:YES];
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
